<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>
    <script src="../js/prop-types.js"></script>
</head>

<body>
    <div id="test"></div>
    <script type="text/babel">
        class Life extends React.Component {
            /* 
                生命周期函数 生命周期钩子
            */
            state = { opacity: 0.3 }

            render() {  //1+n次
                let { opacity } = this.state
                
                return (
                    <div>
                        <h1 style={{ opacity }}>怎么办</h1>
                        <button onClick={this.death}>不活了</button>
                    </div>
                )
            }
        
            //只调用一次  初始化的事
            componentDidMount(){
                console.log('componentDidMount');
                this.timer = setInterval(() => {
                    // 获取原来的opacity
                    let { opacity } = this.state
                    // 递减
                    opacity -= 0.1
                    if (opacity <= 0) opacity = 1
                    // 赋回去
                    this.setState({ opacity })

                }, 200);
            }

            // 
            componentWillUnmount(){
                console.log('componentWillUnmount');
                clearInterval(this.timer)
            }

            death=()=>{
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
        }
        ReactDOM.render(<Life />, document.getElementById('test'))
    </script>
</body>

</html>